package cn.liulin.java8.stream;

import cn.liulin.java8.pojo.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * cn.liulin.java8.stream$
 * 数值流
 * @author ll
 * @date 2021-10-14 14:42:05
 **/
public class NumberStream {

    public static void main(String[] args) {
        Dish dish1 = new Dish("hasd", 100);
        Dish dish2 = new Dish("g", 200);
        Dish dish3 = new Dish("asdf", 300);
        Dish dish4 = new Dish("dfg", 400);
        List<Dish> menu = Arrays.asList(dish1, dish2, dish3, dish4);
//        numberOne(menu);
//        numberTwo(menu);
//        numberThree(menu);
//        numberFour();
        numberFive();
    }

    private static void numberFive() {
        Stream<double[]> stream = IntStream.rangeClosed(0, 100)
                .boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 100)
                        .mapToObj(b -> new double[]{a * a, b * b, Math.sqrt(a * a + b * b)})
                        .filter(b -> b[2] % 1 == 0));
        stream.limit(5).forEach(t -> System.out.println(t[0] +", " + t[1] +", " + t[2]));
    }

    private static void numberFour() {
//        IntStream intStream = IntStream.rangeClosed(1, 100);
        IntStream intStream = IntStream.range(1, 100);
        IntStream intStream1 = intStream.filter(d -> d % 2 == 0);
        System.out.println(intStream1.count());
    }

    private static void numberThree(List<Dish> menu) {
        OptionalInt max = menu.stream()
                .mapToInt(Dish::getCalories)
                .max();
        int i = max.orElse(10000);
        System.out.println(i);
    }

    private static void numberTwo(List<Dish> menu) {
        IntStream intStream = menu.stream()
                .mapToInt(Dish::getCalories);
        int sum = intStream.sum();
        System.out.println(sum);
        Stream<Integer> boxed = intStream.boxed();
    }

    private static void numberOne(List<Dish> menu) {
        Integer reduce = menu.stream()
                .map(Dish::getCalories)
                .reduce(0, Integer::sum);
        System.out.println(reduce);
    }
}
